package com.kgc.est.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.est.constants.Constants;
import com.kgc.est.constants.MaintainStatusEnum;
import com.kgc.est.constants.PayChannelEnum;
import com.kgc.est.mapper.*;
import com.kgc.est.pojo.MaintainOrder;
import com.kgc.est.pojo.MaintainOrderFlow;
import com.kgc.est.pojo.Model;
import com.kgc.est.pojo.PayOrder;
import com.kgc.est.utils.MathUtils;
import com.kgc.est.utils.pay.AliPayConfig;
import com.kgc.est.utils.pay.AlipayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Transactional
@RestController
@RequestMapping("/pay")
public class PayOrderServiceImpl {
    Logger log = LoggerFactory.getLogger(PayOrderServiceImpl.class);

    @Autowired(required = false)
    private PayOrderMapper payOrderMapper;

    @Autowired(required = false)
    private AlipayUtils alipayUtils;

    @Autowired(required = false)
    private MaintainOrderMapper maintainOrderMapper;

    @Autowired(required = false)
    private ModelMapper modelMapper;

    @Autowired(required = false)
    private MaintainOrderFlowMapper maintainOrderFlowMapper;

    @Autowired(required = false)
    private AliPayConfig aliPayConfig;

    /**
     *  1.生成一个支付订单
     *  2.发送支付请求
     * @param orderNo
     */
    @GetMapping("/aliPay")
    public String aliPay(@RequestParam("orderNo") String orderNo){
        log.info("商品订单编号：{}",orderNo);

        QueryWrapper<MaintainOrder> qw = new QueryWrapper<MaintainOrder>();
        qw.eq("orderNo",orderNo);
        MaintainOrder maintainOrder = maintainOrderMapper.selectOne(qw);


        //生成一个支付订单编号
        String payNo = orderNo + MathUtils.random();

        PayOrder payOrder = new PayOrder();
        payOrder.setOrderNo(orderNo);
        payOrder.setPayNo(payNo);
        payOrder.setPrice(maintainOrder.getSubscription());
        payOrder.setPayType(Constants.Order.PayType.DEPOSIT); //支付类型
        payOrder.setStatus(Constants.Order.PayStaus.UN_PAY);
        payOrder.setPayChannel(PayChannelEnum.ALIPAY.getCode());
        payOrder.setCreatedUserId(maintainOrder.getCreatedUserId());
        payOrder.setUserId(maintainOrder.getUserId());
        //保存数据
        payOrderMapper.insert(payOrder);

        //查询商品名称
        Model model = modelMapper.selectById(maintainOrder.getModelId());

        //跳转到支付宝
        String result = alipayUtils.aliPagePay(model.getModelName(),payNo,maintainOrder.getSubscription().toString());

        return result;
    }


    /**
     *  通过支付订单号
     *      1.修改支付订单表中的状态、添加阿里的交易订单号
     *      2.通过查询到的订单表中的订单编号（orderNo），查询维修信息
     *      3.修改维修信息的状态，以及维修详情的状态
     *      4、返回成功后的页面
     * @param map
     * @return
     */
    @PostMapping("/updatePayOrder")
    String updatePayOrder(@RequestBody Map<String,String> map){
        QueryWrapper<PayOrder> qw = new QueryWrapper<>();
        qw.eq("payNo",map.get("out_trade_no"));
        //得到订单对象
        PayOrder payOrder = payOrderMapper.selectOne(qw);

        //添加阿里交易订单编号
        payOrder.setAliTradeNo(map.get("trade_no"));
        payOrder.setStatus(Constants.Order.PayStaus.SUCCESS);
        //在数据库中修改这个对象的值
        payOrderMapper.updateById(payOrder);

        //查询维修订单信息
        QueryWrapper<MaintainOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("orderNo",payOrder.getOrderNo());
        MaintainOrder maintainOrder = maintainOrderMapper.selectOne(wrapper);
        //修改订单状态
        maintainOrder.setStatus(Constants.Order.MaintainStatus.PAY_DEPOSIT);
        maintainOrderMapper.updateById(maintainOrder);

        //修改维修流水信息
        QueryWrapper<MaintainOrderFlow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNo",payOrder.getOrderNo());

        MaintainOrderFlow maintainOrderFlow = maintainOrderFlowMapper.selectOne(queryWrapper);

        //修改订单流水的状态
        maintainOrderFlow.setOrderStatus(MaintainStatusEnum.PAY_DEPOSIT.getCode());
        maintainOrderFlow.setOrderStatusDesc(MaintainStatusEnum.PAY_DEPOSIT.getMsg());
        maintainOrderFlowMapper.updateById(maintainOrderFlow);
        //返回修改成功，页面的路径
        return aliPayConfig.getSuccessUrl();
    }
}
